package com.gss.armory.web;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.gss.armory.form.DocReports;
import com.gss.armory.form.DocumentCustodianDetailsForm;
import com.gss.armory.form.ReportsForm;
import com.gss.armory.service.ReportingService;
import com.gss.common.util.ApplicationMaps;

@Controller
public class ReportsController {
	private Logger logger = Logger.getLogger(ReportsController.class);
	
	@Autowired
	ReportingService reportingService;
	
	@RequestMapping("/getReportsPage")
	public ModelAndView getReportsPage(){
		ModelAndView modelAndView = new ModelAndView("reports/reports");
		ReportsForm reportsForm = new ReportsForm();
		
		modelAndView.addObject("docTypeMap", ApplicationMaps.DOCUMENT_TYPE_ID_MAP);
		modelAndView.addObject("businessUnitMap", ApplicationMaps.LOCID_LOC_MAP);
		modelAndView.addObject("reportsForm", reportsForm);
		
		return modelAndView;
	}
	
	@RequestMapping("loadReportsTable")
	public ModelAndView loadReportsTable(HttpServletRequest httpServletRequest,
			@ModelAttribute ReportsForm reportsForm){
		ModelAndView modelAndView = new ModelAndView("reports/reportsTable");
		
		logger.debug("DOCUMENT NAME: " + reportsForm.getDocName());
		
		List resultOfSelect = reportingService.fetchReports(reportsForm);

		modelAndView.addObject("resultOfSelect", resultOfSelect);
		modelAndView.addObject("typeMap", ApplicationMaps.DOCUMENT_TYPE_ID_NAME_MAP);
		modelAndView.addObject("catMap", ApplicationMaps.DOCUMENT_CAT_ID_MAP);
		modelAndView.addObject("subCatMap", ApplicationMaps.DOCUMENT_SUB_CAT_ID_MAP);
//		modelAndView.addObject("locMap", ApplicationMaps.LOCID_LOCNAME_MAP);
		modelAndView.addObject("locMap", ApplicationMaps.getLocationMapForCurrentUser());
		modelAndView.addObject("deptMap", ApplicationMaps.DEPTID_DEPTSNAME_MAP);
		
		
		return modelAndView;
	}
	
	@RequestMapping("loadExpiredReports")
	public ModelAndView loadExpiredReports(){
		ModelAndView modelAndView = new ModelAndView("reports/expiredReportsFilters");
		
		DocReports docReports = new DocReports();
		
		modelAndView.addObject("docReports", docReports);
		modelAndView.addObject("docTypeMap", ApplicationMaps.DOCUMENT_TYPE_ID_MAP);
//		modelAndView.addObject("businessUnitMap", ApplicationMaps.LOCID_LOC_MAP);
		modelAndView.addObject("businessUnitMap", ApplicationMaps.getLocationMapForReportFilters());
		modelAndView.addObject("companyMap", ApplicationMaps.getCompaniesMappedToUser());
				
		return modelAndView;
	}
	
	@RequestMapping("fetchExpiredDocDataForReports")
	public ModelAndView fetchExpiredDocDataForReports(HttpServletRequest httpServletRequest,@ModelAttribute DocReports docReports){
		ModelAndView modelAndView = new ModelAndView("reports/expiredDocsReportsTable");
		List<DocReports> resultList = reportingService.fetchExpiredDocReports(docReports);
		
		HashMap<Long, DocReports> reportMap = new HashMap<Long, DocReports>();
		
		for(DocReports docReport : resultList){
			if(reportMap.containsKey(docReport.getDocId())){
				reportMap.get(docReport.getDocId()).setAuthSign(docReport.getAuthSign());
			}else {
				reportMap.put(docReport.getDocId(), docReport);
			}
		}
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer
		.append("Authorized Signatory Id: ").append(docReports.getAuthSignId()==null?"(not selected)":docReports.getAuthSignId())
		.append(" | Authorized Signatory Name: ").append(docReports.getAuthSignId()==null?"(not selected)":docReports.getAuthSign())
		.append(" | Business Unit: ").append(docReports.getLdName()==null?"ALL":docReports.getLdName())
		.append(" | Department: ").append(docReports.getDdName()==null?"ALL":docReports.getLdName())
		.append(" | Doc.Type: ").append(docReports.getDtName()==null?"ALL":docReports.getDtName())
		.append(" | Doc.Category: ").append(docReports.getDcName()==null?"ALL":docReports.getDcName())
		.append(" | Doc.Sub-Category: ").append(docReports.getDscName()==null?"ALL":docReports.getDscName());
		
		modelAndView.addObject("resultList", reportMap.values());
		modelAndView.addObject("caption",stringBuffer);
					
		return modelAndView;
	}
	
	@RequestMapping("/loadDocsNearExpiryReports")
	public ModelAndView loadDocsNearExpiryReports(){
		ModelAndView modelAndView = new ModelAndView("reports/nearExpiryReportsFilter");
		
		DocReports docReports = new DocReports();
		
		modelAndView.addObject("docReports", docReports);
		modelAndView.addObject("docTypeMap", ApplicationMaps.DOCUMENT_TYPE_ID_MAP);
//		modelAndView.addObject("businessUnitMap", ApplicationMaps.LOCID_LOC_MAP);
		modelAndView.addObject("businessUnitMap", ApplicationMaps.getLocationMapForReportFilters());
		modelAndView.addObject("companyMap", ApplicationMaps.getCompaniesMappedToUser());
		
		return modelAndView;
	}
	
	@RequestMapping("/fetchNearExpiryDocDataForReports")
	public ModelAndView fetchNearExpiryDocDataForReports(HttpServletRequest httpServletRequest,@ModelAttribute DocReports docReports){
		ModelAndView modelAndView = new ModelAndView("reports/nearExpiryDocReportsTable");
		
		List<DocReports> resultList = reportingService.fetchNearExpiryDocReports(docReports);
		
		HashMap<Long, DocReports> reportMap = new HashMap<Long, DocReports>();
		
		for(DocReports docReport : resultList){
			if(reportMap.containsKey(docReport.getDocId())){
				reportMap.get(docReport.getDocId()).setAuthSign(docReport.getAuthSign());
			}else {
				reportMap.put(docReport.getDocId(), docReport);
			}
		}
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer
		.append("Authorized Signatory Id: ").append(docReports.getAuthSignId()==null?"(not selected)":docReports.getAuthSignId())
		.append(" | Authorized Signatory Name: ").append(docReports.getAuthSignId()==null?"(not selected)":docReports.getAuthSign())
		.append(" | Business Unit: ").append(docReports.getLdName()==null?"ALL":docReports.getLdName())
		.append(" | Department: ").append(docReports.getDdName()==null?"ALL":docReports.getLdName())
		.append(" | Doc.Type: ").append(docReports.getDtName()==null?"ALL":docReports.getDtName())
		.append(" | Doc.Category: ").append(docReports.getDcName()==null?"ALL":docReports.getDcName())
		.append(" | Doc.Sub-Category: ").append(docReports.getDscName()==null?"ALL":docReports.getDscName());
		
		modelAndView.addObject("resultList", reportMap.values());
		modelAndView.addObject("caption",stringBuffer);
		
		return modelAndView;
	}
	
	@RequestMapping("/loadVaultDetailsReports")
	public ModelAndView loadVaultDetailsReports(){
		ModelAndView modelAndView = new ModelAndView("reports/vaultDetailsReportsFilters");
		
		DocumentCustodianDetailsForm detailsForm = new DocumentCustodianDetailsForm();
		
		modelAndView.addObject("detailsForm", detailsForm);
//		modelAndView.addObject("businessUnitMap", ApplicationMaps.LOCID_LOC_MAP);
		modelAndView.addObject("businessUnitMap", ApplicationMaps.getLocationMapForReportFilters());
		modelAndView.addObject("docTypeMap", ApplicationMaps.DOCUMENT_TYPE_ID_MAP);
		
		return modelAndView;
	}
	
	@RequestMapping("/fetchVaultDetailsReports")
	public ModelAndView fetchVaultDetailsReports(HttpServletRequest httpServletRequest,@ModelAttribute DocumentCustodianDetailsForm detailsForm){
		logger.debug("inside reports controller: method(fetchVaultDetailsReports)");
		
		ModelAndView modelAndView = new ModelAndView("reports/vaultDetailsReportsTable");

		List<DocumentCustodianDetailsForm> resultList = reportingService.fetchVaultDetailsReports(detailsForm);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer
		.append("Business Unit (Custodian): ").append(detailsForm.getCustodianLocName()==null?"ALL":detailsForm.getCustodianLocName())
		.append(" | Department (Custodian): ").append(detailsForm.getCustodianDepttName()==null?"ALL":detailsForm.getCustodianDepttName())
		.append(" | Business Unit (Document): ").append(detailsForm.getLdName()==null?"ALL":detailsForm.getLdName())
		.append(" | Department (Document): ").append(detailsForm.getDdName()==null?"ALL":detailsForm.getLdName())
		.append(" | Doc.Type: ").append(detailsForm.getDtName()==null?"ALL":detailsForm.getDtName())
		.append(" | Doc.Category: ").append(detailsForm.getDcName()==null?"ALL":detailsForm.getDcName())
		.append(" | Doc.Sub-Category: ").append(detailsForm.getDscName()==null?"ALL":detailsForm.getDscName());
		
		modelAndView.addObject("resultList", resultList);
		modelAndView.addObject("caption",stringBuffer);
		
		return modelAndView;
	}

}
